﻿using AspNetExpressInformationManageSystem.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace AspNetExpressInformationManageSystem.App_Start
{
    public class ExpressDbInitializer : DropCreateDatabaseIfModelChanges<ExpressDbContext>
    {
        protected override void Seed(ExpressDbContext context)
        {
            InitializeExpressGoods(context);
            InitializeExpressStorage(context);
            InitializeExpressGoodsStorage(context);
            InitializeExpressVehicle(context);
            base.Seed(context);
        }

        private void InitializeExpressGoods(ExpressDbContext context)
        {
            var goods = new List<ExpressGoods>()
            {
                new ExpressGoods
                {
                    ID = 1,
                    SenderName = "寄件人A",
                    SenderTel = "1234567",
                    SenderAddr = "寄件人A地址",
                    SenderZipCode = "123456",
                    ReceiverName = "收件人A",
                    ReceiverTel = "1234567",
                    ReceiverAddr = "收件人A地址",
                    ReceiverZipCode = "123456",
                    Weight = 10,
                    Size = 10,
                    Cost = 10,
                    Remark = "",
                    Status = "已收件",
                    Date = DateTime.Now.Date
                },
                new ExpressGoods
                {
                    ID = 2,
                    SenderName = "寄件人B",
                    SenderTel = "1234567",
                    SenderAddr = "寄件人B地址",
                    SenderZipCode = "123457",
                    ReceiverName = "收件人B",
                    ReceiverTel = "1234567",
                    ReceiverAddr = "收件人B地址",
                    ReceiverZipCode = "123457",
                    Weight = 10,
                    Size = 10,
                    Cost = 10,
                    Remark = "",
                    Status = "已收件",
                    Date = DateTime.Now.Date
                }
            };
            context.ExpressGoods.AddRange(goods);
        }

        private void InitializeExpressStorage(ExpressDbContext context)
        {
            var storages = new List<ExpressStorage>()
            {
                new ExpressStorage
                {
                    ID = 1,
                    Name = "仓库A",
                    Addr = "仓库A地址",
                    Type = "中转仓库",
                    ZipCode = "123456",
                    Size = 1000,
                    Manager = "admin@example.com"
                },
                new ExpressStorage
                {
                    ID = 2,
                    Name = "仓库B",
                    Addr = "仓库B地址",
                    Type = "终点仓库",
                    ZipCode = "123457",
                    Size = 1000,
                    Manager = "admin@example.com"
                }
            };
            context.ExpressStorage.AddRange(storages);
        }

        private void InitializeExpressGoodsStorage(ExpressDbContext context)
        {
            var goodsStorages = new List<ExpressGoodsStorage>()
            {
                new ExpressGoodsStorage
                {
                    ID = 1,
                    ExpressGoodsID = 1,
                    ExpressStorageID = 1,
                    Action = "入库",
                    Operator = "admin@example.com",
                    Status = "在库",
                    Date = DateTime.Now.Date
                }
            };
            context.ExpressGoodsStorage.AddRange(goodsStorages);

            foreach (var item in goodsStorages)
            {
                var goods = context.ExpressGoods.Find(Convert.ToInt32(item.ExpressGoodsID));
                if (goods != null)
                {
                    goods.Status = "运往仓库A";
                }
            }
        }

        public void InitializeExpressVehicle(ExpressDbContext context)
        {
            var vehicles = new List<ExpressVehicle>()
            {
                new ExpressVehicle
                {
                    ID = 1,
                    PlateNum = "NB001",
                    Load = 1000,
                    Size = 1000,
                    Operator = "管理员",
                    Maintenance = 10,
                    OilCost = 10,
                    Status = "运营中"
                }
            };
            context.ExpressVehicles.AddRange(vehicles);
        }
    }
}